package com.juli.school.service.utils;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.juli.school.dao.entity.Lottery;

/**
 * @Description: 抽奖生成奖项
 * @author: wjp
 * @date: 2018年3月25日上午10:20:58
 */
public class BigWheelDrawUtil {
	/**
	 * 给转盘的每个角度赋初始值
	 * 
	 * @return
	 */
	private final static List<Lottery> initDrawList = new ArrayList<Lottery>() {
		{
			add(new Lottery(1, "200", 1));
			add(new Lottery(2, "100", 3));
			add(new Lottery(3, "50", 30));
			add(new Lottery(4, "30", 30));
			add(new Lottery(5, "20", 26));
			add(new Lottery(6, "10", 10));
		}
	};

	/**
	 * 生成奖项
	 * 
	 * @return
	 */
	public static Lottery generateAward(List<Lottery> initData) {
		int index = 10000;// 设置抽奖范围，1/index
		long result = randomnum(1, index);
		int line = 0;
		int temp = 0;
		Lottery returnobj = null;
		for (int i = 0; i < initData.size(); i++) {
			Lottery obj2 = initData.get(i);
			int c = obj2.getProbability();
			temp = temp + c;
			line = index - temp;
			if (c != 0) {
				if (result > line && result <= (line + c)) {
					returnobj = obj2;
					break;
				}
			}
		}
		return returnobj;
	}

	// 获取2个值之间的随机数
	private static long randomnum(int smin, int smax) {
		int range = smax - smin;
		double rand = Math.random();
		return (smin + Math.round(rand * range));
	}

	public static void main(String[] args) {
		System.out.println(JSON.toJSONString(generateAward(initDrawList)));
	}

}
